package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.AnnounceVO;
import com.bbzn.device.client.constant.AnnounceConstant;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.dataobject.AnnounceUser;
import com.bbzn.device.client.dataobject.Announcement;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.dto.announce.AnnouncementDTO;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.AnnounceUserService;
import com.bbzn.device.client.service.AnnouncementService;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.*;
import com.bbzn.user.comment.User;
import com.bbzn.user.comment.res.UserRes;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wulongbo
 * @Date 2021/12/22 16:05
 * @Version 1.0
 */

@RestController
@RequestMapping("/announcement")
public class AnnouncementController extends BaseApiService {

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private AnnounceUserService announceUserService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    /**
     * 查询公告列表
     *
     * @return
     */
    @PostMapping(value = "/getAnnouncementList")
    public BaseResponse getAnnouncementList(HttpServletRequest request, @RequestBody AnnounceVO reqVO) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        AnnouncementDTO dto = new AnnouncementDTO();
        if (null != reqVO.getPageNum()) {
            dto.setPageNum(reqVO.getPageNum());
        }
        if (null != reqVO.getPageSize()) {
            dto.setPageSize(reqVO.getPageSize());
        }
        dto.setToUserId(userId);
        dto.setFromUserId(userId);
        dto.setStartIndex((reqVO.getPageNum() - 1) * reqVO.getPageSize());
        PageInfo<AnnouncementDTO> dtos = announcementService.findPageByAll(dto);
        return setResultSuccess(dtos);
    }


    /**
     * 新增公告
     *
     * @return
     */
    @PostMapping(value = "/addAnnouncement")
    public BaseResponse addAnnouncement(HttpServletRequest request, @RequestBody AnnounceVO reqVO) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        String userName = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getTitle()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_TITLE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getContent()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_CONTENT_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (CollectionUtils.isEmpty(reqVO.getIds())) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_IDS_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Announcement announcement = new Announcement();
        announcement.setFromUserId(userId);
        announcement.setContent(reqVO.getContent());
        announcement.setTitle(reqVO.getTitle());
        announcement.setCreateBy(userName);
        announcement.setUpdateBy(userName);
        announcementService.insertSelective(announcement);
        List<AnnounceUser> announceUsers = new ArrayList<>();
        if(null!=announcement.getId()){
            announceUsers = reqVO.getIds().stream().parallel().map(id->new AnnounceUser(announcement.getId(),id)).collect(Collectors.toList());
        }
        if(CollectionUtils.isNotEmpty(announceUsers)){
            announceUserService.batchInsert(announceUsers);
        }
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 更新公告
     *
     * @return
     */
    @PostMapping(value = "/updateAnnouncement")
    public BaseResponse updateAnnouncement(HttpServletRequest request, @RequestBody AnnounceVO reqVO) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        String userName = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getId()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getTitle()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_TITLE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getContent()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_CONTENT_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Announcement announcement = new Announcement();
        announcement.setId(reqVO.getId());
        announcement.setContent(reqVO.getContent());
        announcement.setTitle(reqVO.getTitle());
        announcement.setUpdateBy(userName);
        announcementService.updateByPrimaryKeySelective(announcement);
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 更新公告
     *
     * @return
     */
    @PostMapping(value = "/delAnnouncement")
    public BaseResponse delAnnouncement(HttpServletRequest request, @RequestBody AnnounceVO reqVO) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getId()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(AnnounceConstant.ANNOUNCE_ENUM, AnnounceConstant.ANNOUNCE_NO_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        AnnounceUser del = new AnnounceUser();
        del.setAnnouncementId(reqVO.getId());
        announceUserService.deleteSelective(del);
        announcementService.deleteByPrimaryKey(reqVO.getId());
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 公告获取用户信息
     *
     * @return
     */
    @PostMapping(value = "/getAnnounceUserList")
    public BaseResponse getAnnounceUserList(HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        User query = new User();
        query.setId(userId);
        List<UserRes> userRes = commonService.getChildByUser(query);
        return setResultSuccess(userRes);
    }
}
